//
//  WebViewController.m
//  Rong360
//
//  Created by tianliwei on 8/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "WebViewController.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "UMSocialWechatHandler.h"
#import "LoanCalculatorViewController.h"
#import "RealEstateQaViewController.h"
#import "TaxesCalculatorViewController.h"
#import "FWFSViewController.h"
@interface WebViewController ()
@property (nonatomic, copy)NSString *localPath;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *url;
@property (nonatomic, copy)NSString *desc;
@property (nonatomic, strong)NSURLRequest* request;
@property (nonatomic, strong)UIActivityIndicatorView *webLoading;
@end

@implementation WebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithUrl:(NSString *)url title:(NSString *)title desc:(NSString *)desc{
    self = [super init];
    if (self) {
        self.title = title;
        self.desc = [NSString stringWithFormat:@"%@ %@",desc,url];
        self.url = url;
        self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];//创建NSURLRequest
    }
    return self;
}

- (id)initWithUrl:(NSString *)url title:(NSString *)title{
    self = [super init];
    if (self) {
        self.title = title;
        self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];//创建NSURLRequest
    }
    return self;
}

- (void)share{
    
    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
    [UMSocialWechatHandler setWXAppId:kWXAppID url:self.url];
    [UMSocialConfig setQQAppId:kTencentID url:self.url importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
//    [UMSocialConfig setShareQzoneWithQQSDK:YES url:self.url importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:kUMSocialKey
                                      shareText:self.desc
                                     shareImage:[UIImage imageNamed:kShareIcon]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,nil]
                                       delegate:nil];
}

- (void)pressReturn{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
	// Do any additional setup after loading the view.
    self.view.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT);
    self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    if (self.type == HTML5ShareUrl) {
        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [shareButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        
        [shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
        shareButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        shareButton.frame = CGRectMake(0.0f, 0.0f, 18.0f, 25.0f);
        UIBarButtonItem *historyButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
        self.navigationItem.rightBarButtonItem = historyButtonItem;
    }
    if (self.push == YES) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setBackgroundImage:[UIImage imageNamed:@"navi_backbtn"] forState:UIControlStateNormal];
        [backButton setTitle:@"" forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(pressReturn) forControlEvents:UIControlEventTouchUpInside];
        backButton.frame = CGRectMake(0.0f, 0.0f, 25.0f, 25.0f);
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = backButtonItem;
    }
    //使用loadHTMLString()方法显示网页内容
//    NSURL *baseURL =[[NSBundle mainBundle]bundleURL];
    
    [self.webView loadRequest:self.request];//加载
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    
    self.webLoading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.webLoading.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT);
    [self.webLoading startAnimating];
    [self.view addSubview:self.webLoading];

}
//读取html文件内容
- (NSString *)getHtmlString{
    //文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:self.localPath ofType:@"html" inDirectory:@"localHtml"];
    
    NSString *contents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    return contents;
}

- (void )webViewDidFinishLoad:(UIWebView  *)webView{
    [self.webLoading removeFromSuperview];
//    NSString *string = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];
//    NSLog(@"===> %@",string);
}

- (void)webView:(UIWebView *)webView  didFailLoadWithError:(NSError *)error{
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *url = [request URL];
        NSString *curUrl= [url absoluteString];
        NSLog(@"cururl: %@",curUrl);
        if ([curUrl isEqualToString:@"http://m.rong360.com/LoanCalculator"]) {
            self.hidesBottomBarWhenPushed = YES;
            LoanCalculatorViewController *loanVC = [[LoanCalculatorViewController alloc]init];
            [self.navigationController pushViewController:loanVC animated:YES];
            return NO;
        }else if ([curUrl isEqualToString:@"http://m.rong360.com/shouertao"]){
            self.hidesBottomBarWhenPushed = YES;
            RealEstateQaViewController *qaVC = [[RealEstateQaViewController alloc]init];
            [self.navigationController pushViewController:qaVC animated:YES];
            return NO;
        }else if ([curUrl isEqualToString:@"http://m.rong360.com/TaxesCalculator"]){
            self.hidesBottomBarWhenPushed = YES;
            TaxesCalculatorViewController *taxesVC = [[TaxesCalculatorViewController alloc]init];
            [self.navigationController pushViewController:taxesVC animated:YES];
            return NO;
        }else if ([curUrl isEqualToString:@"http://m.rong360.com/FangWuFengShui"]){
            self.hidesBottomBarWhenPushed = YES;
            FWFSViewController *fwfsVC = [[FWFSViewController alloc]init];
            [self.navigationController pushViewController:fwfsVC animated:YES];
            return NO;
        }else if ([curUrl isEqualToString:@""]){
            //
        }else if ([curUrl isEqualToString:@""]){
            //
        }
    }
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
